package org.opsli.modulars.business.historyTimeTrail.web;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.wrapper.business.realTimeTrail.HistoryTimeTrailModel;
import org.opsli.api.wrapper.business.realTimeTrail.RealTimeTrailModel;
import org.opsli.common.annotation.ApiRestController;
import org.opsli.core.base.controller.BaseRestController;
import org.opsli.core.persistence.Page;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.business.app.web.CoordinatesConvertUtil;
import org.opsli.modulars.business.app.web.MapCordinatesVo;
import org.opsli.modulars.business.historyTimeTrail.entity.HistoryTimeTrail;
import org.opsli.modulars.business.historyTimeTrail.service.HistoryTimeTrailService;
import org.opsli.modulars.business.realTimeTrail.entity.RealTimeTrail;
import org.opsli.modulars.business.realTimeTrail.service.RealTimeTrailService;
import org.opsli.modulars.business.realTimeTrail.service.impl.RealTimeTrailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@ApiRestController("/patrolRecord/historyTimeTrail")
public class HistoryTimeTrailController extends BaseRestController<HistoryTimeTrail, HistoryTimeTrailModel, HistoryTimeTrailService> {

    @Autowired
    private HistoryTimeTrailService rt;

    @RequestMapping(value = "/findUserHistoryTime", method = RequestMethod.POST)
    public ResultVo<?> getUser(Integer pageNo, Integer pageSize,String username) {
        String devid = UserUtil.getSysUser().getCompanyId();

/*        System.out.println("state::"+state_EQ);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String nowDateBegin = null;
        String nowDateEnd = null;
        //当进行查询操作的时候，对传入的时间进行处理
        try {
            if (StringUtils.isNotBlank(beginTime_BEGIN)) {
                long ts = df.parse(beginTime_BEGIN).getTime();
                long now = ts + 86400000;
                Date date0 = new Date(now);
                nowDateBegin = df.format(date0);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        try {
            if (StringUtils.isNotBlank(endTime_END)) {
                long ts = df.parse(endTime_END).getTime();
                long now = ts + 86400000;
                Date date0 = new Date(now);
                nowDateEnd = df.format(date0);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("beginTime::"+nowDateBegin);
        System.out.println("endTime::"+nowDateEnd);*/

        System.out.println(username);
        List<HistoryTimeTrail> list = rt.findTask(devid,username);

        list.sort(Comparator.comparing(HistoryTimeTrail::getCreTime).reversed());
        Page page = new Page(pageNo, pageSize);
        int totalLength = list.size(); //总的数据条数
        int totalPage = totalLength % pageSize == 0 ? totalLength / pageSize : totalLength / pageSize + 1; //总的页数
        List pageData = null;
        if (pageNo < totalPage) {
            int startIndex = (pageNo - 1) * pageSize;
            int endIndex = startIndex + pageSize - 1;
            pageData = list.subList(startIndex, endIndex + 1);
        } else {
            int startIndex = (pageNo - 1) * pageSize;
            pageData = list.subList(startIndex, totalLength);//最后一页
        }
        page.setList(pageData);
        page.setTotal(list.size());
        return ResultVo.success(page.getPageData());
    }

    @RequestMapping(value = "/findUserHistoryTimeTrial", method = RequestMethod.POST)
    public ResultVo<?> getTrail(String sign) {
        List list = new ArrayList();
        List l = rt.fingTrail(sign);
        String trail = null;
        if (l.size() > 0) {
            for (int i = 0; i < l.size(); i++) {
                HistoryTimeTrail r = (HistoryTimeTrail) l.get(i);
                trail = r.getTrail();
            }
        }
        if (StringUtils.isNotBlank(trail)) {
            JSONArray jsonArray = JSONArray.parseArray(trail);
            for (int v = 0; v < jsonArray.size(); v++) {
                Map m = new HashMap();
                String lat = String.valueOf(jsonArray.getJSONObject(v).get("ln"));
                String lng = String.valueOf(jsonArray.getJSONObject(v).get("lt"));
                MapCordinatesVo cordinatesVo = new MapCordinatesVo();
                cordinatesVo.setLon(new BigDecimal(lng));
                cordinatesVo.setLat(new BigDecimal(lat));
                cordinatesVo = CoordinatesConvertUtil.bd_encrypt(cordinatesVo);
                m.put("lt", cordinatesVo.getLon());
                m.put("ln", cordinatesVo.getLat());
                list.add(m);
            }
        }
        return ResultVo.success(list);
    }
}
